package edu.nctu.csie.jichang.dp.behavioral;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Command {
	public static void main(String[] args) {
		CmdMaps tCmdMaps = new CmdMaps();
		tCmdMaps.addCmd("space",new SkipSpaceCmd());
		tCmdMaps.addCmd("uppercase",new SkipUpperCaseCmd());
		String tMsg = "Asd fGH ;";
		String[] tCmds = new String[]{"uppercase","space"};
		System.out.println(tMsg + " => " + tCmdMaps.doExecute(tMsg,Arrays.asList(tCmds)) );
	}
}
class CmdMaps {
	Map<String, Cmd> cmdMaps = new HashMap<String, Cmd>();
	public void addCmd(String pName,Cmd pCmd) {
		cmdMaps.put(pName, pCmd);
	}
	public String doExecute(String str,List<String> pCmds) {
		String r = str;
		for(String s: pCmds) {
			r = cmdMaps.get(s).doExecute(r);
		}
		return r;
	}
}
interface Cmd{
	String doExecute(String pContent);
}
class SkipSpaceCmd implements Cmd{
	public String doExecute(String pContent) {
		return pContent.replaceAll(" ", "");
	}
}
class SkipUpperCaseCmd implements Cmd{
	public String doExecute(String pContent) {
		return pContent.toLowerCase();
	}
}
